package com.duan.search.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMqConfig {


    //队列
    public static final String AD_QUEUE = "ad.queue";

    //商品队列
    public static final String GOODS_QUEUE = "goods.queue";

    //商品队列
    public static final String GOODS_DOWN_QUEUE = "goods.down.queue";

    //交换机
    public static final String AD_EXCHANGE = "ad.exchange";

    //商品交换机
    public static final String GOODS_EXCHANGE = "goods.exchange";

    //商品交换机
    public static final String GOODS_DOWN_EXCHANGE = "goods.down.exchange";


    //声明队列
    /*
     *   new Queue(QUEUE_EMAIL,true,false,false)
     *   durable="true" 持久化 rabbitmq重启的时候不需要创建新的队列
     *   auto-delete 表示消息队列没有在使用时将被自动删除 默认是false
     *   exclusive  表示该消息队列是否只在当前connection生效,默认是false
     */
    @Bean(AD_QUEUE)
    public Queue AD_QUEUE() {
        return new Queue(AD_QUEUE,true);
    }

    @Bean(GOODS_QUEUE)
    public Queue GOODS_QUEUE() {
        return new Queue(GOODS_QUEUE,true);
    }

    @Bean(GOODS_DOWN_QUEUE)
    public Queue GOODS_DOWN_QUEUE() {
        return new Queue(GOODS_DOWN_QUEUE,true);
    }

    //声明交换机
    @Bean(AD_EXCHANGE)
    public Exchange AD_EXCHANGE() {
        //durable(true) 持久化，mq重启之后交换机还在
        return ExchangeBuilder.fanoutExchange(AD_EXCHANGE).durable(true).build();
    }

    @Bean(GOODS_EXCHANGE)
    public Exchange GOODS_EXCHANGE() {
        //durable(true) 持久化，mq重启之后交换机还在
        return ExchangeBuilder.fanoutExchange(GOODS_EXCHANGE).durable(true).build();
    }

    @Bean(GOODS_DOWN_EXCHANGE)
    public Exchange GOODS_DOWN_EXCHANGE() {
        //durable(true) 持久化，mq重启之后交换机还在
        return ExchangeBuilder.fanoutExchange(GOODS_DOWN_EXCHANGE).durable(true).build();
    }

    //队列绑定交换机，指定routingKey
    @Bean
    public Binding AD_EXCHANGE_QUEUE(@Qualifier(AD_QUEUE) Queue queue, @Qualifier(AD_EXCHANGE) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(AD_QUEUE).noargs();
    }

    @Bean
    public Binding GOODS_EXCHANGE_QUEUE(@Qualifier(GOODS_QUEUE) Queue queue, @Qualifier(GOODS_EXCHANGE) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(GOODS_QUEUE).noargs();
    }

    @Bean
    public Binding GOODS_DOWN_EXCHANGE_QUEUE(@Qualifier(GOODS_DOWN_QUEUE) Queue queue, @Qualifier(GOODS_DOWN_EXCHANGE) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(GOODS_DOWN_QUEUE).noargs();
    }
}

